let ieee = {}


ieee.loadInited = false;
ieee.desktopLastIndex = -1; // 用于记录上一次处理索引。
ieee.mobileLastIndex = -1; //
ieee.storage =[]  //存储函数的


ieee.start=function(){
   let interval = setInterval(function() {
       if(!ieee.isLoading()){
            clearInterval(interval);
            // for desktop page select
           $("#xplMainContent > div.ng-SearchResults.row > div.main-section > xpl-paginator a").click(function(){
               ieee.desktopLastIndex = -1;
               ieee.mobileLastIndex = -1;
               ieee.start();
           });
            if (!ieee.loadInited){
                ieee.loadInited = true;
                $(".loadMore-btn").click(ieee.start);
            }
            ieee.appending_all()
       }
   },1000)

}


ieee.appending_all = function(){
    // for desktop
    ieee.fetchHTML().then(function (html) {
        let result =  ieee.fetchData(html)
        console.log(result)
        for (let i = 0; i < result.length; i++) {
            ieee.appending($(result[i]))
        }

    })
    //
    // $(".description > div:nth-child(1) > a").each(function(index){
    //     if(index > ieee.mobileLastIndex) {
    //         ieee.mobileLastIndex = index;
    //         console.log($(this).text());
    //         ieee.appending($(this));
    //     }
    // });
}

ieee.fetchHTML=function () {
    return new Promise(function (resolve, reject) {
        chrome.tabs.executeScript({
            code: 'document.body.innerHTML'
        }, function (results) {
            if (chrome.runtime.lastError) {
                reject(chrome.runtime.lastError);
            } else {
                resolve(results[0]);
            }
        });
    });
}
ieee.fetchData=function (html) {
    var parser = new DOMParser();
    var xmlDoc = parser.parseFromString(html, 'text/html');
    var xpathResult = xmlDoc.evaluate('//xpl-results-item/div[1]/div[1]/div[2]/div/a', xmlDoc, null, XPathResult.ANY_TYPE, null);
    var textValues = [];
    // 遍历XPath结果
    var aElement = xpathResult.iterateNext();
    while (aElement) {
        // 获取<a>元素之间的文本内容
        var textContent = aElement.textContent.trim();
        // 存储文本内容
        textValues.push(textContent);
        // 获取下一个<a>元素
        aElement = xpathResult.iterateNext();
    }
    return textValues
}

ieee.isLoading = function() {
    return $('.List-results-message').text().trim() === "Getting results..."
        || $('xpl-progress-spinner').text().trim() === "Getting results..."
        || $(".loadMore-btn > span.fa-spinner").length === 1
}

ieee.appending = function(result) {
    let source = result.text().trim();
    if(source.length !== 0){
        let names = ieee.parseNames(source);
        console.log(names);
        for(let getRankingSpan of ieee.storage){
            result.before(getRankingSpan(names[0]["full"].toUpperCase()));
        }
    }
    console.log(result);
}

ieee.parseNames = function(source){
    let names = [];
    let index = source.lastIndexOf('(');
    let full;
    if(index!== -1){
        full = source.substring(0, index);
    } else {
        full = source;
    }
    full = del_digit.removeNumbers(full).split(";");
    for(let i = 0; i < Math.max(full.length); ++i){
        let name = {};
        name.full = (full[i] || "").trim();
        names.push(name);
    }
    return names;
}






